package day1219;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Server2 {

    class  ClientService  extends  Thread{
        Socket  socket;//需要给线程里边传入一个Socket(获取输入流和输出流)
        //输出流存入到集合，用输入流读取数据
        ClientService(Socket  s){
            socket=s;
        }
        public void run() {
            //用socket获取输入流(读取客户端的数据)和输出流(放入集合)(从集合中获取每个输出流)
            //用输出流写出数据(每个客户端返回数据)
            PrintWriter  pw=null;
            try {
                InputStream in=socket.getInputStream();
                InputStreamReader isr=new InputStreamReader(in,"utf-8");
                BufferedReader  br=new BufferedReader(isr);

                OutputStream  out=socket.getOutputStream();//从socket中获取输出流
//                OutputStreamWriter osw=new OutputStreamWriter(out,"UTF-8");
//                pw=new PrintWriter(osw);
                OutputStreamWriter osw=new OutputStreamWriter(out,"UTF-8");
                //BufferedWriter bw=new BufferedWriter(osw);
                pw=new PrintWriter(osw);
                //将输出流放入集合
                synchronized (allOut){
                    allOut.add(pw);
                    System.out.println("在线的人数为：" + allOut.size());
                }

                String msg=null;
                while(  (msg=br.readLine()) !=null   ){//while里边，用输入读取数据
                    System.out.println(msg);//打印某个客户端发送的数据
                    //从集合中获取每个客户端输出流，输出msg给每个客户端
                    synchronized (allOut){
                        for(int i=0;i<allOut.size();i++){
                            PrintWriter  pwFromAll=allOut.get(i);
                            System.out.println("给所有客户端返回数据msg");
                            pwFromAll.println(msg);
                            pwFromAll.flush();
                        }
                    }

                }
                //用集合中的每个输出流给每个客户端返回数据
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                //如果某个客户端关闭了就把它对应的输出流从集合中删除
                synchronized (allOut){
                    allOut.remove(pw);
                    System.out.println("在线的人数为：" + allOut.size());
                }
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }




    ArrayList<PrintWriter>   allOut=new ArrayList();//保存每个客户端对应的输出流(通讯录)

    public void start(){
        try {
            while(true){
                System.out.println("服务器等待客户端连接...");
                s=ss.accept();//2.接收客户端的连接
                System.out.println(s.getInetAddress()+"连接成功了");

                //客户端启动成功，启动一个线程为此客户端服务(和此客户端通信)
                ClientService  cs=new ClientService(s);
                cs.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Socket s=null;
    ServerSocket   ss=null;//1.用来充当服务器端
    Server2(){
        System.out.println("正在启动服务器...");
        try {
            ss=new ServerSocket(8089);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("启动服务器成功...");
    }




    public static void main(String[] args) {
        Server2 ser=new Server2();//1.创建服务器端对象
        ser.start();//2.用服务器端对象调用start()
    }
}


